<?php
namespace Home\Model;
use Think\Model;

/**
* 保存数据库
*/
class UserModel extends Model
{
	public function saveData($getInfo)
	{
		$userModel = M('story');
        $data['openid'] = $getInfo['openid'];
        $data['content'] = $getInfo['content'];
        $userreturn = $userModel->add($data);
        if ($userreturn>0) {
        return true;
       }else{
        return false;
       }
	}


    public function modifyData($getInfo)
    {
        $userModel = M('story');
        $openid = $getInfo['openid'];
        $data['content'] = $getInfo['content'];
        $userreturn = $userModel->where("openid = '%s'",$openid)->save($data);
        //var_dump($userreturn);
        if ($userreturn>0) {
            //echo "success";
             return true;
       }else{
             //echo "false";
             return false;
       }
    }

	
    public function checkOpenid($openid)//检测摆渡人的人的openid
    {
        $total = 0;
    	$userModel = M('story');
        $userreturn = $userModel->where("touser1 = '%s'",$openid)->find();
        if ($userreturn != 0) {
            $total = $total + 1;
        }
        $userreturn = $userModel->where("touser2 = '%s'",$openid)->find();
        if ($userreturn != 0) {
             $total = $total + 1;
        }
        if ($total == 2) {
            return false;
        }
        return true;
    }
     //
     //false 表示故事的人的openid存在
     //true  表示故事的人的openid不存在
    public function checksendOpenid($openid)//检测发故事的人的openid
    {
        $userModel = M('story');
        $userreturn = $userModel->where("openid = '%s'",$openid)->count();
         //var_dump($userreturn);
        return $userreturn;
        
        
    }

    public function findContent($openid)
    {
    	$userModel = M('story');
        $userreturn = $userModel->where('time<2')->count();
        //var_dump($userreturn);
        if($userreturn == 0) //查找是否存在数字 不存在 返回false
        {
            $return['success'] == false;
            echo "content is null";
            return $return;
            exit();
        }
        $userreturn = $userModel->where('time<2')->find();
        $time = $userreturn['time'];
        
        //var_dump($userreturn);
        if ($time == 0) {
            $time = $time+1;
            $data['touser1'] = $openid;
            $data['time'] = $time;
            $userModel->where("openid = '%s'",$userreturn['openid'])->save($data);
            $return['success'] = true;
            $return['content'] = $userreturn['content'];
            //var_dump($return);
            return $return;
            exit();
        } else if ($time == 1) {
            if ($userreturn['touser1'] == $openid) {
                $map['time']  = array('lt','2');
                $map['touser1']  = array('neq',$openid);
                $userreturn = $userModel->where($map)->find();
            }

            $time = $time+1;
            $data['touser2'] = $openid;
            $data['time'] = $time;
            //$userModel->where("openid = '%s' and touser1 <> '$s'",$userreturn['openid'],$openid)->save($data);
            $userModel->where("openid = '%s'",$userreturn['openid'])->save($data);
            $return['success'] = true;
            $return['content'] = $userreturn['content'];
            //var_dump($return);
            return $return;
            exit();
        }

    }


}